package com.taotao.portal.service.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.HttpClientUtil;
import com.taotao.common.utils.JsonUtils;
import com.taotao.order.pojo.Order;
import com.taotao.portal.service.CartService;
import com.taotao.portal.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService {

	@Value("${ORDER_BASE_URL}")
	private String ORDER_BASE_URL;
	@Value("${ORDER_CREATE}")
	private String ORDER_CREATE;
	@Value("${ORDER_INFO}")
	private String ORDER_INFO;
	@Autowired
	private CartService cartService;
	
	@Override
	public Order createOrder(Order order,HttpServletRequest request, HttpServletResponse response) throws Exception {
		String url=ORDER_BASE_URL+ORDER_CREATE;
		String json = JsonUtils.objectToJson(order);
		String data = HttpClientUtil.doPostJson(url, json);
		if(StringUtils.isBlank(data))
			throw new Exception("获取数据失败");
		TaotaoResult taotaoResult = TaotaoResult.formatToPojo(data, String.class);
		String orderId=(String) taotaoResult.getData();
		
		Order result = new Order();
		result.setOrderId(orderId);
		result.setPayment(order.getPayment());
		cartService.clearCart(request, response);
		return result;
	}

}
